package com.pigdogbay.androidutils.usercontrols;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class NumberPicker extends LinearLayout implements DialogInterface.OnDismissListener{

	INumberPickerValue _NumberPickerValue;
	INumberEditorDialog _NumberEditorDialog;
	Button _DisplayButton;
	
	public void setNumberPickerValue(INumberPickerValue numberPickerValue)
	{
		_NumberPickerValue = numberPickerValue;
		_DisplayButton.setText(_NumberPickerValue.getFormattedString());
	}
	public void setNumberEditorDialog(INumberEditorDialog numberEditorDialog)
	{
		if (_NumberEditorDialog!=null)
		{
			_NumberEditorDialog.setOnDismissListener(null);
		}
		_NumberEditorDialog = numberEditorDialog;
		_NumberEditorDialog.setOnDismissListener(this);
	}
	
	public NumberPicker(Context context) {
		this(context,null);
	}
	public NumberPicker(Context context, AttributeSet attributeSet) {
		super(context,attributeSet);
		this.setOrientation(HORIZONTAL);
		_NumberPickerValue = new NumberPickerValue();
		setNumberEditorDialog(new NumberEditorDialog(context, "Enter Value", "Enter Value"));
		
		Button minusButton = new Button(context);
		minusButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				minusButtonClicked();
			}
		});
		minusButton.setText("-");
		minusButton.setMinEms(2);
		minusButton.setTextAppearance(context,
				android.R.style.TextAppearance_Large);
		minusButton.setTypeface(null, Typeface.BOLD);
		LinearLayout.LayoutParams minBtnLayParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		
		Button plusButton = new Button(context);
		plusButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				plusButtonClicked();
			}
		});
		plusButton.setText("+");
		plusButton.setMinEms(2);
		plusButton.setTextAppearance(context,
				android.R.style.TextAppearance_Large);
		plusButton.setTypeface(null, Typeface.BOLD);
		
		_DisplayButton = new Button(context);
		_DisplayButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				displayButtonClicked();
			}
		});
		_DisplayButton.setText("12st 5lb");
		_DisplayButton.setMinEms(6);
		_DisplayButton.setTextAppearance(context,
				android.R.style.TextAppearance_Large);
		
		addView(minusButton, minBtnLayParams);
		addView(_DisplayButton, minBtnLayParams);
		addView(plusButton,minBtnLayParams);
	
	}
	
	private void minusButtonClicked()
	{
		if (_NumberPickerValue!=null)
		{
			_NumberPickerValue.decrease();
			_DisplayButton.setText(_NumberPickerValue.getFormattedString());
		}
	}
	private void plusButtonClicked()
	{
		if (_NumberPickerValue!=null)
		{
			_NumberPickerValue.increase();
			_DisplayButton.setText(_NumberPickerValue.getFormattedString());
		}
	}
	private void displayButtonClicked()
	{
		if (_NumberEditorDialog!=null)
		{
			_NumberEditorDialog.show(_NumberPickerValue);
		}
		_DisplayButton.setText(_NumberPickerValue.getFormattedString());
	}
	@Override
	public void onDismiss(DialogInterface dialog) {
		_DisplayButton.setText(_NumberPickerValue.getFormattedString());
	}

}
